const nodemailer = require('nodemailer');
const ejs = require('ejs');
const fs = require('fs');
const path = require('path');
const transporter = nodemailer.createTransport({
	host: 'smtp.qq.com',
	port: 465,
	secure: true, // 是否使用TLS，true，端口为465，否则其他或者568
	auth: {
		user: '990748875@qq.com',
		pass: 'quislqjtasjqbcji'
	}
});

const sendMail = (to, code) => {
	return new Promise((resolve, reject) => {
		transporter.sendMail(
			{
				from: '990748875@qq.com',
				to,
				subject: '您有一封来自【ZYL博客】的回执！',
				html: ejs.render(fs.readFileSync(path.resolve(__dirname, '../views/email.ejs')).toString(), { code })
			},
			(err, info) => {
				if (err) {
					reject(err);
				} else {
					resolve(info);
				}
			}
		);
	});
};

module.exports = sendMail;
